/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.contribute.pihome;

import be.contribute.common.controllers.PeripheralController;
import be.contribute.common.pojo.Temperature;
import be.contribute.common.pojo.TextMessage;
import be.contribute.pihome.devices.DeviceController;
import java.sql.Timestamp;
import java.util.List;

/**
 *
 * @author debian
 */
public class PiHome implements Runnable{
    
    private DBController fDBController;
    private PeripheralController fPeripheralController;
    private final int fSleepTimeInMillies = 30001;

    public PiHome() {
    }

    @Override
    public void run() {
        fDBController = DBController.getInstance();
        fPeripheralController = PeripheralController.getInstance();
       // boolean succes = fPeripheralController.initGSM();
        boolean succes = true;
        while(succes)
        {
            try {
                Thread.sleep(fSleepTimeInMillies);
                List<Temperature> tempList = fPeripheralController.readOwnTemperature();
                Temperature averageTemp = Temperature.averageFromList(tempList);
                if(averageTemp != null)
                    fDBController.insertIntoTemperature(averageTemp);
                
                /*List<TextMessage> messageList = fPeripheralController.readTexts();
                if(messageList.size() > 0)
                {
                    for(TextMessage msg: messageList)
                    {
                        System.out.println("SUCCES! " + msg.toString());
                    }
                }*/
            } catch (InterruptedException ex) {
                 ex.printStackTrace();
            }
        }
    }
}
